﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using UIControls;
using BO;
using UIControls;

namespace UI.Core
{
    public partial class FormBase : WeifenLuo.WinFormsUI.Docking.DockContent
    {

        public FormBase()
        {
            InitializeComponent();            
            this.ImeMode = System.Windows.Forms.ImeMode.OnHalf;
            this.Font = GlobalUIVar.gFont;

            this.BackColor = GlobalUIVar.gBackColor;
        }
        public bool IsSecurityMode = false;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (Utility.IsDesignMode())
                return;
            #region //设置权限.
            var t=this.GetType();
            var flist = Utility.GetAllControl(t, this);
            var alist = GlobalVar.User.ReadRoleAccessList;
            foreach (var f in flist)
            {
                var c = f.GetValue(this) as Control;

                if (c == null || c is IUIControl == false)
                    continue;
                var ac = alist.FindControlAccess(t.FullName, c.Name);
                if (ac != null)              
                {
                    if (ac.Enabled)
                    {
                        c.Enabled = false;
                        c.Visible = true;
                    }
                    if (ac.Visible)
                    {
                        c.Visible = false;
                        c.Enabled = true;
                    }
                }
            }
            #endregion
        }
      

        #region 修正输入法全角/半角的问题
      //  声明一些API函数   
        [DllImport("imm32.dll")]
        public static extern IntPtr ImmGetContext(IntPtr hwnd);
        [DllImport("imm32.dll")]
        public static extern bool ImmGetOpenStatus(IntPtr himc);
        [DllImport("imm32.dll")]
        public static extern bool ImmSetOpenStatus(IntPtr himc, bool b);
        [DllImport("imm32.dll")]
        public static extern bool ImmGetConversionStatus(IntPtr himc, ref   int lpdw, ref   int lpdw2);
        [DllImport("imm32.dll")]
        public static extern int ImmSimulateHotKey(IntPtr hwnd, int lngHotkey);
        private const int IME_CMODE_FULLSHAPE = 0x8;
        private const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11;
        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            IntPtr HIme = ImmGetContext(this.Handle);
         //   如果输入法处于打开状态  
            if (ImmGetOpenStatus(HIme))
            {
                int iMode = 0;
                int iSentence = 0;
              //  检索输入法信息   
                bool bSuccess = ImmGetConversionStatus(HIme, ref   iMode, ref   iSentence);
                if (bSuccess)
                {
                  //  如果是全角,转换成半角  
                    if ((iMode & IME_CMODE_FULLSHAPE) > 0)
                        ImmSimulateHotKey(this.Handle, IME_CHOTKEY_SHAPE_TOGGLE);
                }

            }
        }
        #endregion 修正输入法全角/半角的问题
     
       
    }
}
